<?php

header("Content-type:text/html; charset=utf-8;");
require '../../../ppf/fun.php';
require '../../../ppf/pdo_mysql.php';
require '../../../ppf/Filter.php';
/* p($_POST);
  var_dump(json_decode($_POST['attachment']));
  exit; */
$pd = new pdo_mysql();
$filter = new Filter();
session_start();

if (empty($_SESSION['uid'])) {
    $tips = array(
        'tips' => '请登录后再进行操作',
        'url' => 'index.php'
    );
    $tips = urlencode(serialize($tips));
    header('Location:/tips.php?gets=' . $tips);
    exit;
}

$submitMethod = $_SERVER["REQUEST_METHOD"];
if ($submitMethod == 'POST') {

    $title = empty($_POST['title']) ? "" : Filter::safe_string($_POST['title']);
    $description = empty($_POST['description']) ? "" : Filter::safe_string($_POST['description']);
    $copy_to_id = empty($_POST['copy_to_id']) ? "" : Filter::safe_string($_POST['copy_to_id']);
    $attachment = '';
    $attachment_id = '';
    $attachment_name = '';
    if ($_POST['attachment']) {
        $attachment_info = json_decode($_POST['attachment'], TRUE);
        foreach ($attachment_info as $k => $v) {
            $attachment_name.=Filter::safe_string($v['attachment_name']) . "*";
            $attachment_id.=Filter::safe_string($v['attachment_id']) . ",";
        }
    }
    if (!empty($_POST['attachment_old'])) {
        $attachment_info = json_decode($_POST['attachment_old'], TRUE);
        foreach ($attachment_info as $k => $v) {
            $attachment_name.=Filter::safe_string($v['attachment_name']) . "*";
            $attachment_id.=Filter::safe_string($v['attachment_id']) . ",";
        }
    }
    $microtime = explode(" ", microtime());
    $id = $microtime[1] + $microtime[0];
    date_default_timezone_set('PRC'); //设置中国时区 
    $in_data = array(
        'title' => $title,
        'description' => $description,
        'from_id' => $_SESSION['username'],
        'attachment_name' => $attachment_name,
        'attachment_id' => $attachment_id,
        'copy_to_id' => $copy_to_id,
        'uploadtime' => date("Y-m-d H:i:s"),
        "id" => $id,
    );
    if ($pd->insert(array('data' => $in_data, 'table' => 'file_upload'))) {
        if (!empty($_POST['SMS_REMIND'])) {
            foreach ($_POST['SMS_REMIND'] as $value) {
                //使用内部短信提醒用户
                if ($value == 1) {
                    
                } else {  //使用手机短信提醒用户
                }
            }
        }
        $tips = array(
            'tips' => '发送成功',
            'url' => './?t=file_send'
        );
        $tips = urlencode(serialize($tips));
        header('Location:/tips.php?gets=' . $tips);
        //echo("<script language='javascript'>window.parent.location.href='/tips.php?gets=" . $tips . "'</script>");
    } else {
        echo "添加失败";
        exit;
    }
} else {
    $tips = array(
        'tips' => '不允许的表单提交方式，请按正常流程提交表单。',
        'url' => 'index.php'
    );
    $tips = urlencode(serialize($tips));
    //echo("<script language='javascript'>window.parent.location.href='/tips.php?gets=" . $tips . "'</script>");
    header('Location:/tips.php?gets=' . $tips);
}
?>